﻿/*
 * Copyright © 2020, Wuxi Safover Intelligent Technology Co., Ltd. All rights reserved.
 * 
 * 无锡安永智能科技有限公司版权所有，未经许可，不得用于商业用途
 * 
 * Visit: https://www.safover.com/ 
 */

using SafoverWpfFw;
using System.Collections.Generic;
using System.Windows.Input;
using Safover.LidarCollector.Managers;
using Safover.LidarCollector.Models;
using Safover.LidarCollector.Config;
using static Safover.LidarCollector.Managers.UiDeviceController;

namespace Safover.LidarCollector.ViewModel
{
    public class DeviceControlPanelViewModel : BaseViewModel
    {
        public bool IsPreiodicalCollecting
        {
            get
            {
                return DataCollector.Instance.IsCollectorThreadWorking;
            }
        }

        #region Laser

        public ICommand StartLasarCommand { get; set; }
        public ICommand StopLasarCommand { get; set; }

        public ICommand ErrorResetLasarCommand { get; set; }

        public ICommand SwitchOpticalMaserCommand { get; set; }

        private void SwitchOpticalMaser(object obj)
        {
            if (obj is bool)
            {
                var isOpened = (bool)obj;

                if (isOpened)
                {
                    Instance.CloseOpticalMaser();
                }
                else
                {
                    Instance.OpenOpticalMaser();
                }
            }
        }

        #endregion

        #region Acquisition Card
        public ICommand InitAcquisitionCardCommand { get; set; }

        public ICommand ResetAcquisitionCardCommand { get; set; }

        public string AcquisitionCardParams { get; }

        public int SamplingDepth { get; set; }

        public int CumulativePulses { get; set; }


        public ICommand SwitchAcquisitionCardCommand { get; set; }

        private void SwitchAcquisitionCard(object obj)
        {
            if (obj is bool)
            {
                var isOpened = (bool)obj;

                if (isOpened)
                {
                    Instance.CloseAcquisitionCard();
                }
                else
                {
                    Instance.OpenAcquisitionCard();
                }
            }
        }

        #endregion 

        #region ElectronicCompass
        public ICommand QueryCompassAnglesCommand { get; set; }

        public ICommand SwitchElectronicCompassCommand { get; set; }

        private void SwitchElectronicCompass(object obj)
        {
            if (obj is bool)
            {
                var isOpened = (bool)obj;

                if (isOpened)
                {
                    Instance.CloseElectronicCompass();
                }
                else
                {
                    Instance.OpenElectronicCompass();
                }
            }
        }

        public float? PitchAngle { get; set; }
        public float? HeadingAngle { get; set; }
        public float? RollAngle { get; set; }

        private void QueryCompassAngles()
        {
            ElectronicCompassAngles? angles = Instance.QueryAngles();

            if (angles != null)
            {
                ElectronicCompassAngles temp = (ElectronicCompassAngles)angles;

                RollAngle = temp.RollAngle;
                HeadingAngle = temp.HeadingAngle;
                PitchAngle = temp.PitchAngle;
            }
            else
            {
                RollAngle = null;
                HeadingAngle = null;
                PitchAngle = null;
            }
        }

        #endregion

        #region Rotary Table

        public float VerticalAngle { get; set; }

        public float HorizontalAngle { get; set; }

        public bool IsRotaryTableMoving => DI.ViewModelDeviceReadings.IsRotaryTableMoving;


        //public ICommand MoveRotaryTableCommand { get; set; }

        public ICommand StopMovingRotaryTableCommand { get; set; }

        public float[] VerticalRange { get; }
        public float[] HorizontalRange { get; }

        public ICommand MoveVerticallyToCommand { get; set; }
        public ICommand MoveHorizontallyToCommand { get; set; }

        public ICommand QueryVerticalAngleCommand { get; set; }
        public ICommand QueryHorizontalAngleCommand { get; set; }

        public ICommand SwitchRotaryTableCommand { get; set; }

        private void SwitchRotaryTable(object obj)
        {
            if (obj is bool)
            {
                var isOpened = (bool)obj;

                if (isOpened)
                {
                    Instance.CloseRotaryTable();
                }
                else
                {
                    Instance.OpenRotaryTable();
                }
            }
        }

        #endregion

        #region GPS Locator

        public double? Latitude { get; set; }

        public double? Longitude { get; set; }

        public double? Altitude { get; set; }


        public ICommand SwitchGPSLocatorCommand { get; set; }

        public ICommand QueryGPSCommand { get; set; }

        private void SwitchGPSLocator(object obj)
        {
            if (obj is bool)
            {
                var isOpened = (bool)obj;

                if (isOpened)
                {
                    Instance.CloseGpsLocator();
                }
                else
                {
                    Instance.OpenGpsLocator();
                }
            }
        }

        private void QueryGPS()
        {
            var gpsInfo = Instance.QueryGpsInformation();

            if (gpsInfo != null)
            {
                Latitude = gpsInfo.Latitude;
                Longitude = gpsInfo.Longitude;
                Altitude = gpsInfo.Altitude;
            }
        }

        #endregion

        public string ControlResult { get; set; }

        private bool AbleToControl(object o) 
        {
            return !IsPreiodicalCollecting;
        }

        public DeviceControlPanelViewModel()
        {
            SwitchOpticalMaserCommand = new RelayParameterizedCommand(SwitchOpticalMaser, AbleToControl);
            SwitchAcquisitionCardCommand = new RelayParameterizedCommand(SwitchAcquisitionCard, AbleToControl);
            SwitchRotaryTableCommand = new RelayParameterizedCommand(SwitchRotaryTable, AbleToControl);
            SwitchElectronicCompassCommand = new RelayParameterizedCommand(SwitchElectronicCompass, AbleToControl);
            SwitchGPSLocatorCommand = new RelayParameterizedCommand(SwitchGPSLocator, AbleToControl);

            StartLasarCommand = new RelayCommand(() => ControlResult = Instance.StartLasar(), AbleToControl);

            StopLasarCommand = new RelayCommand(() => ControlResult = Instance.StopLasar(), AbleToControl);


            ErrorResetLasarCommand = new RelayCommand(() => ControlResult = Instance.ErrorResetLaser(), AbleToControl);

            InitAcquisitionCardCommand = new RelayCommand(() => Instance.InitAcquisitionCardParams(SamplingDepth, CumulativePulses),
                AbleToControl);

            ResetAcquisitionCardCommand = new RelayCommand(() => Instance.ResetAcquisitionCard(), AbleToControl);

            CumulativePulses = RuntimeConfigManager.Instance.AcquisitionCardConfig.CumulativePulses;
            SamplingDepth = RuntimeConfigManager.Instance.AcquisitionCardConfig.SamplingDepth;
            AcquisitionCardParams = $"采样点数=[{SamplingDepth}]; 累加次数=[{CumulativePulses}]";

            var verticalRange = new List<float>();
            for (int i = 0; i <= 90; i++)
            {
                verticalRange.Add(i);
            }
            VerticalRange = verticalRange.ToArray();

            var horizontalRange = new List<float>();
            for (int i = 0; i < 360; i++)
            {
                horizontalRange.Add(i);
            }
            HorizontalRange = horizontalRange.ToArray();

            MoveVerticallyToCommand = new RelayCommand(MoveVerticallyTo, AbleToControl); 

            MoveHorizontallyToCommand = new RelayCommand(MoveHorizontallyTo, AbleToControl);

            QueryVerticalAngleCommand = new RelayCommand(() => ControlResult = Instance.QueryVerticalAngle());
            QueryHorizontalAngleCommand = new RelayCommand(() => ControlResult = Instance.QueryHorizontalAngle());

            StopMovingRotaryTableCommand = new RelayCommand(() => ControlResult = Instance.StopMovingRotaryTable(), AbleToControl);

            QueryCompassAnglesCommand = new RelayCommand(QueryCompassAngles);

            QueryGPSCommand = new RelayCommand(QueryGPS);
        }

        

        private void MoveVerticallyTo()
        {
            ControlResult = Instance.MoveVerticallyTo(VerticalAngle);
        }

        private void MoveHorizontallyTo()
        {
            ControlResult = Instance.MoveHorizontallyTo(HorizontalAngle);
        }

        
    }
}
